﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QUAN_LY_MUON_TRA_SACH
{
    public partial class frmthemtacgia : Form
    {
        public frmthemtacgia()
        {
            InitializeComponent();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand lenh = new SqlCommand("insert into tac_gia values('" + txtmatacgia.Text + "'" + "," + "N'" + txthoten.Text + "'" + "," + "'" + cadngaysinh.Text + "'" + "," + "N'" + cbogioitinh.Text + "'" + "," + "N'" + txthocvi.Text + "')", Connection.sqlConnection);
                int n = lenh.ExecuteNonQuery();
                MessageBox.Show(n + " tác giả mới đã được thêm!", "Cập nhật thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                lenh.Dispose();
                FORM_MAIN.frmmain.btnds_Click(sender, e);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txtmatacgia.Clear();
            txthoten.Clear();
            cadngaysinh.Text="";
            cbogioitinh.Text = "";
            txthocvi.Clear();
            //tu dong sinh ma tac gia moi
            SqlCommand lenh = new SqlCommand("select max(cast(substring(ma_tgia,3,len(ma_tgia)-2) as int))+1 from tac_gia", Connection.sqlConnection);
            if (Convert.ToInt32(lenh.ExecuteScalar()) < 100)
                txtmatacgia.Text = "TG00" + Convert.ToString(lenh.ExecuteScalar());
            else if (Convert.ToInt32(lenh.ExecuteScalar()) >= 100 && Convert.ToInt32(lenh.ExecuteScalar()) < 1000)
                txtmatacgia.Text = "TG0" + Convert.ToString(lenh.ExecuteScalar());
            else
                txtmatacgia.Text = "TG" + Convert.ToString(lenh.ExecuteScalar());
        }

        private void frmthemtacgia_Load(object sender, EventArgs e)
        {
            //tu dong sinh ma tac gia moi
            SqlCommand lenh = new SqlCommand("select max(cast(substring(ma_tgia,3,len(ma_tgia)-2) as int))+1 from tac_gia", Connection.sqlConnection);
            if (Convert.ToInt32(lenh.ExecuteScalar()) < 100)
                txtmatacgia.Text = "TG00" + Convert.ToString(lenh.ExecuteScalar());
            else if (Convert.ToInt32(lenh.ExecuteScalar()) >= 100 && Convert.ToInt32(lenh.ExecuteScalar()) < 1000)
                txtmatacgia.Text = "TG0" + Convert.ToString(lenh.ExecuteScalar());
            else
                txtmatacgia.Text = "TG" + Convert.ToString(lenh.ExecuteScalar());
        }
    }
}
